/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.dao.hibernate;

import br.com.fatecpg.songs.entity.Song;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import static org.hibernate.criterion.MatchMode.ANYWHERE;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Eloyza
 */
public class SongDAO extends SearchableDAO<Song> implements br.com.fatecpg.songs.dao.SongDAO {

    public SongDAO() {
        super(Song.class);
    }
    
    /* Consulta de músicas por titulo, genero, titulo do album,
       nome do compositor, nome do artista, letra ou tradução da letra. 
    */

    @Override
    public void setAlias(Criteria c) {
        c.createAlias("genre", "g", Criteria.LEFT_JOIN)
                .createAlias("album", "a", Criteria.LEFT_JOIN)
                .createAlias("composers", "c", Criteria.LEFT_JOIN)
                .createAlias("a.artist", "art", Criteria.LEFT_JOIN);
    }

    @Override
    public void setOrder(Criteria c) {
        c.addOrder(Order.asc("title"))
                .addOrder(Order.asc("art.name"));
    }

    @Override
    public Criterion getSearchCriterion(String searchedWord) {
        Criterion criterion = Restrictions.or(
                Restrictions.ilike("title", searchedWord, ANYWHERE),
                Restrictions.or(
                        Restrictions.ilike("a.title", searchedWord, ANYWHERE),
                        Restrictions.or(
                                Restrictions.ilike("c.name", searchedWord, ANYWHERE),
                                Restrictions.or(
                                        Restrictions.ilike("art.name", searchedWord, ANYWHERE),
                                        Restrictions.ilike("lyrics", searchedWord, ANYWHERE)
                                )
                        )
               )
        );
        return criterion;
    }    
}
